#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int v[220][220];
int Ford(int vs,int vt)
{
    bool done[220];
    int pos,father[220],min,sum=0,i;
    while(1)
    {
        //fill(done,done+220,0);
        fill(father,father+220,-1);
        queue<int> q;
        done[vs]=true;
        q.push(vs);
        while(!q.empty())
        {
            pos=q.front();
            q.pop();
            for(i=1;i<=vt;i++)
            {
                if(v[pos][i]!=0&&!done[i])
                {
                    done[i]=true;
                    father[i]=pos;
                    q.push(i);
                }
            }
            if(done[vt]) break;
        }
        if(!done[vt]) break;
        for(i=vt,min=0xFFFF;i!=vs;i=father[i])
        {
            if(v[father[i]][i]<min)
            {
                min=v[father[i]][i];
            }
        }
        sum+=min;
        for(i=vt;i!=vs;i=father[i])
        {
            v[father[i]][i]-=min;
            v[i][father[i]]+=min;
        }
    }
    return sum;
}
int main()
{
    int N,M,i,from,to,w;
    while(cin>>N>>M)
    {
        fill(v[0],v[0]+220*220,0);
        for(i=0;i<N;i++)
        {
            cin>>from>>to>>w;
            v[from][to]+=w;
        }
        cout<<Ford(1,M)<<endl;
    }
    return 0;
}
